<template>
  <div class="admin-volunteers">
    <h2>志愿者管理</h2>
    
    <!-- 查询表单 -->
    <el-form :inline="true" class="search-form">
      <el-form-item label="姓名">
        <el-input v-model="searchForm.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="班级">
        <el-input v-model="searchForm.className" placeholder="请输入班级" />
      </el-form-item>
      <el-form-item label="二级学院">
        <el-input v-model="searchForm.department" placeholder="请输入二级学院" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="filteredVolunteers">
      <el-table-column prop="volunteerId" label="志愿者编号" width="100" />
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column prop="gender" label="性别" width="80" />
      <el-table-column prop="className" label="班级" width="120" />
      <el-table-column prop="department" label="二级学院" width="150" />
      <el-table-column prop="joinDate" label="加入时间" width="180" />
      <el-table-column prop="phone" label="联系电话" width="120" />
      <el-table-column prop="email" label="电子邮箱" />
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button 
            type="primary"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog v-model="dialogVisible" title="编辑志愿者信息">
      <el-form :model="volunteerForm" label-width="100px">
        <el-form-item label="志愿者编号">
          <el-input v-model="volunteerForm.volunteerId" disabled />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="volunteerForm.name" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="volunteerForm.gender">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="volunteerForm.className" />
        </el-form-item>
        <el-form-item label="二级学院">
          <el-input v-model="volunteerForm.department" />
        </el-form-item>
        <el-form-item label="加入时间">
          <el-date-picker v-model="volunteerForm.joinDate" disabled />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="volunteerForm.phone" />
        </el-form-item>
        <el-form-item label="电子邮箱">
          <el-input v-model="volunteerForm.email" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getUsers, updateUserProfile } from '@/api/user'

export default {
  data() {
    return {
      volunteers: [],
      searchForm: {
        name: '',
        className: '',
        department: ''
      },
      dialogVisible: false,
      volunteerForm: {
        volunteerId: '',
        name: '',
        gender: '男',
        className: '',
        department: '',
        joinDate: '',
        phone: '',
        email: ''
      }
    }
  },
  created() {
    this.fetchVolunteers()
  },
  methods: {
    async fetchVolunteers() {
      try {
        const data = await getUsers(this.searchForm)
        this.volunteers = data
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleSave() {
      try {
        await updateUserProfile(this.volunteerForm.volunteerId, this.volunteerForm)
        this.dialogVisible = false
        this.$message.success('保存成功')
        this.fetchVolunteers()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
}
</style> 